package com.mrjy.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.mrjy.common.entity.SMSSendInfo;
import com.mrjy.common.entity.SMSSendReqInfo;
import com.mrjy.maintain.dao.ISMSSendInfoDao;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class SMSUtil {
	
	private static final String APP_Key = "23461686";
	
	private static final String APP_SECRET = "8317b95ef42391f5e6701741bba49eae";
	
	private static final String SMS_URL = "http://gw.api.taobao.com/router/rest";
	
	@Resource
	private static ISMSSendInfoDao smsSendInfoDao;
	
	//短信签名，传入的短信签名必须是在阿里大鱼“管理中心-短信签名管理”中的可用签名
	public static final String[] SIGN_NAME = new String[]{"明人家庭教育"};
	
	public static Map<String,Object> sendSMS(SMSSendReqInfo smsSendReq) {
		Map<String,Object> map = new HashMap<String, Object>();
		TaobaoClient client = new DefaultTaobaoClient(SMS_URL, APP_Key, APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		SMSTemplate smsTemplate = smsSendReq.getsmsTemplate();		
		String templateContent = smsTemplate.getTemplateContent();
		Pattern p = Pattern.compile("\\$\\{.*?\\}");// 查找规则公式中${}以内的字符
		Matcher m = p.matcher(templateContent);
		StringBuffer sb = new StringBuffer();
		while (m.find()) {// 遍历找到的所有的${}
			String param = m.group().replaceAll("\\$\\{", "").replaceAll("\\}", "");// 去掉${}
			sb.append(",").append("\"").append(param).append("\":").append("\"")
					.append(smsSendReq.getsmsParamMap().get(param).toString()).append("\"");
		}
		if (sb.length() > 0) {
			req.setSmsParamString("{" + sb.deleteCharAt(0) + "}");
		} else {
			req.setSmsParamString(null);
		}
		req.setExtend(smsSendReq.getExtend());
		req.setSmsType(smsSendReq.getSmsType());
		req.setSmsFreeSignName(smsSendReq.getSmsFreeSignName());		
		req.setRecNum(smsSendReq.getRecNum());
		req.setSmsTemplateCode(smsTemplate.getTemplateID());
		try {
			AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
			if (StringUtils.isBlank(rsp.getErrorCode())) {
				map.put("responseCode", ResponseCode.SUCCESS.getCode());
				map.put("responseDesc", rsp.getBody());
			} else {
				map.put("responseCode", rsp.getSubCode());
				map.put("responseDesc", rsp.getSubMsg());
			}
		} catch (ApiException e) {
			e.printStackTrace();			
			map.put("responseCode","404");
			map.put("responseDesc", "发送信息失败！");
			
		}
		return map;
	}
	
	
	public static void main(String[] args) {		
		String smsType = "normal";
		String smsFreeSignName = SMSUtil.SIGN_NAME[0];
		String recNum = "18825182418";
		Map<String,Object> smsParamMap = new HashMap<String, Object>();
		//String product = "明人科技有限公司";	
		smsParamMap.put("name", "陈少水");
		smsParamMap.put("income", 91.25f);
		smsParamMap.put("total", 168168.25f);
		SMSSendReqInfo smsSendReq = new SMSSendReqInfo(smsType,
				smsFreeSignName, smsParamMap, recNum, SMSTemplate.TEMPLATE_11);
		try {
			Map<String,Object> map = SMSUtil.sendSMS(smsSendReq);
			if (map.get("responseCode") == ResponseCode.SUCCESS.getCode()) {
				JSONObject  jsonObject = JSONObject.fromObject(map.get("responseDesc"));				
				jsonObject = JSONObject.fromObject(jsonObject.getString("alibaba_aliqin_fc_sms_num_send_response"));
				jsonObject = JSONObject.fromObject(jsonObject.getString("result"));
				System.out.println(jsonObject.getString("model"));//打印短信发送流水
									
			} else {
				System.out.println(map.get("responseCode"));
				System.out.println(map.get("responseDesc"));
			}									
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();			
		}
	}
	
}
